﻿using Microsoft.VisualStudio.Modeling;
using Microsoft.VisualStudio.Modeling.Diagrams;
using System.Collections;
using System.Linq;

namespace Project.Co_DataTransformationPrototype
{
    #region NamedElement class utilities
    public partial class NamedElement
    {
        /// <summary>
        /// Storage for CurrentName
        /// </summary>
        private global::System.String currentnamePropertyStorage = string.Empty;

        /// <summary>
        /// Get custom CurrentName property
        /// </summary>
        /// <returns></returns>
        internal string GetCurrentNameValue()
        {
            // Piggyback off of Name property
            if (!string.IsNullOrEmpty(this.Name) && !string.Equals(this.Name, currentnamePropertyStorage))
                currentnamePropertyStorage = this.Name;

            return currentnamePropertyStorage;
        }

        /// <summary>
        /// Set custom CurrentName property
        /// </summary>
        /// <param name="value"></param>
        internal void SetCurrentNameValue(string value)
        {
            if (!Store.InUndoRedoOrRollback && !string.IsNullOrEmpty(value.Trim()))
                currentnamePropertyStorage = value;
        }
    }
    #endregion

    #region Entity class utilities
    public partial class Entity
    {
        /// <summary>
        /// Return the name of the entity when the ToString method is invoked
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return this.Name;
        }
    }
    #endregion

    #region EntityAttribute class utilities
    public partial class EntityAttribute
    {
        #region CurrentParentEntityID Methods
        /// <summary>
        /// Storage for CurrentParentEntity
        /// </summary>
        private global::System.Guid currententityidPropertyStorage = System.Guid.Empty;

        /// <summary>
        /// Get custom currentEntity property
        /// </summary>
        /// <returns></returns>
        internal System.Guid GetCurrentEntityIDValue()
        {
            // Piggyback off of Entity property
            if (this.Entity != null && !System.Guid.Equals(this.Entity.Id, currententityidPropertyStorage))
                currententityidPropertyStorage = this.Entity.Id;

            return currententityidPropertyStorage;
        }

        /// <summary>
        /// Set custom currentEntity property
        /// </summary>
        /// <param name="newValue"></param>
        internal void SetCurrentEntityIDValue(System.Guid newValue)
        {
            if (!Store.InUndoRedoOrRollback && !newValue.Equals(System.Guid.Empty))
                currententityidPropertyStorage = newValue;
        }
        #endregion

        #region ReferencedEntityID Methods
        /// <summary>
        /// Storage for ReferencedEntity
        /// </summary>
        private global::System.Guid referencedentityidPropertyStorage = System.Guid.Empty;

        /// <summary>
        /// Get custom referencedEntity property
        /// </summary>
        /// <returns></returns>
        internal System.Guid GetReferencedEntityIDValue()
        {
            return referencedentityidPropertyStorage;
        }

        /// <summary>
        /// Set custom referencedEntity property
        /// </summary>
        /// <param name="newValue"></param>
        internal void SetReferencedEntityIDValue(System.Guid newValue)
        {
            if (!Store.InUndoRedoOrRollback && !newValue.Equals(System.Guid.Empty))
                referencedentityidPropertyStorage = newValue;
        }
        #endregion

        /// <summary>
        /// Return the name of the attribute when the ToString method is invoked
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return this.Name;
        }
        
    }
    #endregion

    #region EntityShape class utilities
    public partial class EntityShape
    {
        protected override CompartmentMapping[] GetCompartmentMappings(System.Type melType)
        {
            CompartmentMapping[] mappings = base.GetCompartmentMappings(melType);
            ElementListCompartmentMapping operationMapping = mappings[0] as ElementListCompartmentMapping;

            operationMapping.ElementListGetter = GetList;
            operationMapping.ImageGetter = GetElementImage;
            
            return mappings;
        }

        private IList GetList(ModelElement startElement)
        {
            return GetElementsFromEntityForAttributes(startElement).OfType<EntityAttribute>().OrderBy(a => a.CurrentName).ToList();
        }
               
        /// <summary>
        /// Decides what the icon of the method will be in the interface shape
        /// </summary>
        /// <param name="mel"></param>
        /// <returns></returns>
        private System.Drawing.Image GetElementImage(ModelElement mel)
        {
            const string resourceName = "ImageResources";
            const string resourcePath = @".\Resources\";
            System.Drawing.Image imgResult = null;
            string nameImage = "Default_Attribute";
            EntityAttribute entry = mel as EntityAttribute;

            if (entry != null)
            {
                System.Resources.ResourceManager resourceMgr = System.Resources.ResourceManager.CreateFileBasedResourceManager(resourceName, resourcePath, null);

                if (entry.isPrimaryKey)
                    nameImage = "PrimaryKey_Attribute";
                else if (entry.isForeignKey)
                    nameImage = "DataContract_Field_Protected";

                // TODO: Use strongly type resource file entries to avoid fileNotFound exceptions for new entries
                imgResult = System.Drawing.Image.FromFile(resourcePath + nameImage + ".bmp");
            }

            return imgResult;
        }
    }
    #endregion

}
